package com.dubito.broccoli

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import me.samlss.broccoli.Broccoli
import me.samlss.broccoli.BroccoliGradientDrawable
import me.samlss.broccoli.PlaceholderParameter
import java.util.*

class RecyclerViewActivity : AppCompatActivity() {

    private val sImageIds = intArrayOf(
        R.mipmap.photo_1, R.mipmap.photo_2,
        R.mipmap.photo_3, R.mipmap.photo_4,
        R.mipmap.photo_5
    )

    private val sPrices = intArrayOf(
        549, 1499, 1199, 1699, 3388
    )

    private val sTitles = arrayOf(
        "honor/荣耀 畅玩7",
        "Huawei/华为 畅想MAX",
        "honor/荣耀 荣耀9i",
        "Huawei/华为 畅想9 PLUS",
        "Huawei/华为 P20"
    )

    private val sDescriptions = arrayOf(
        "2018.05上市 | 免举证退换货",
        "2018.10上市 | 免举证退换货",
        "2018.06上市 | 免举证退换货",
        "2018.10上市 | 免举证退换货",
        "2018.04上市 | 免举证退换货"
    )
    private lateinit var mRecyclerView: RecyclerView
    private lateinit var myAdapter: Adapter
    private val mDataList: MutableList<DataBean> = ArrayList()
    private val mViewPlaceholderManager: MutableMap<View, Broccoli> = HashMap()
    private val mTaskManager: MutableMap<View, Runnable> = HashMap()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view)
        initData()
        initRecyclerView()
    }

    override fun onDestroy() {
        super.onDestroy()
        mDataList.clear()

        for (view in mTaskManager.keys) {
            view.removeCallbacks(mTaskManager[view])
        }

        //Prevent memory leaks when using BroccoliGradientDrawable
        //防止使用BroccoliGradientDrawable时内存泄露
        for (broccoli in mViewPlaceholderManager.values) {
            broccoli.removeAllPlaceholders()
        }

        mViewPlaceholderManager.clear()
        mTaskManager.clear()
    }

    private fun initData() {
        for (i in 0..19) {
            val dataBean: DataBean =
                DataBean()
            dataBean.imageRes = sImageIds[i % sImageIds.size]
            dataBean.title = sTitles[i % sTitles.size]
            dataBean.description = sDescriptions[i % sDescriptions.size]
            dataBean.price = sPrices[i % sPrices.size]
            mDataList.add(dataBean)
        }
    }

    private fun initRecyclerView() {
        mRecyclerView = findViewById(R.id.recyclerView)
        mRecyclerView.layoutManager = LinearLayoutManager(applicationContext)
        myAdapter = Adapter()
        mRecyclerView.adapter = myAdapter
    }


    private inner class Adapter : RecyclerView.Adapter<ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view: View =
                layoutInflater.inflate(R.layout.recycler_view_item, parent, false)
            return ViewHolder(view)
        }

        override fun getItemCount() = mDataList.size

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            var broccoli = mViewPlaceholderManager[holder.itemView]
            if (broccoli == null) {
                broccoli = Broccoli()
                mViewPlaceholderManager[holder.itemView] = broccoli
            }
            broccoli.removeAllPlaceholders()
//            holder.tvTitle.addPlaceholder()
//            holder.imageView.addPlaceholder()
//            holder.tvDescription.addPlaceholder()
//            holder.tvPrice.addPlaceholder()
            broccoli.addPlaceholder(
                PlaceholderParameter.Builder()
                    .setView(holder.tvTitle)
                    .setDrawable(
                        BroccoliGradientDrawable(
                            Color.parseColor("#DDDDDD"),
                            Color.parseColor("#CCCCCC"),
                            0f,
                            1000,
                            LinearInterpolator()
                        )
                    )
                    .build()
            )
            broccoli.addPlaceholder(
                PlaceholderParameter.Builder()
                    .setView(holder.imageView)
                    .setDrawable(
                        BroccoliGradientDrawable(
                            Color.parseColor("#DDDDDD"),
                            Color.parseColor("#CCCCCC"),
                            0f,
                            1000,
                            LinearInterpolator()
                        )
                    )
                    .build()
            )

            broccoli.addPlaceholder(
                PlaceholderParameter.Builder()
                    .setView(holder.tvPrice)
                    .setDrawable(
                        BroccoliGradientDrawable(
                            Color.parseColor("#DDDDDD"),
                            Color.parseColor("#CCCCCC"),
                            0f,
                            1000,
                            LinearInterpolator()
                        )
                    )
                    .build()
            )
            broccoli.addPlaceholder(
                PlaceholderParameter.Builder()
                    .setView(holder.tvDescription)
                    .setDrawable(
                        BroccoliGradientDrawable(
                            Color.parseColor("#DDDDDD"),
                            Color.parseColor("#CCCCCC"),
                            0f,
                            1000,
                            LinearInterpolator()
                        )
                    )
                    .build()
            )
            broccoli.show()



            //delay to show the data
            var task = mTaskManager[holder.itemView]
            if (task == null) {
                val finalBroccoli: Broccoli = broccoli
                task =
                    Runnable { //when you need to update data, you must to call the remove or the clear method.
                        finalBroccoli.removeAllPlaceholders()
                        if (mDataList.isEmpty()) {
                            return@Runnable
                        }
                        holder.imageView.setImageResource(mDataList[position].imageRes)
                        holder.tvPrice.text = "¥" + mDataList[position].price
                        holder.tvTitle.text = mDataList[position].title
                        holder.tvDescription.text = mDataList[position].description
                    }
                mTaskManager[holder.itemView] = task
            } else {
                holder.itemView.removeCallbacks(task)
            }
            holder.itemView.postDelayed(task, 3000)
        }

    }


    private class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.iv_image)
        val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        val tvDescription: TextView = itemView.findViewById(R.id.tv_description)
        val tvPrice: TextView = itemView.findViewById(R.id.tv_price)

    }

    private class DataBean {
        var imageRes = 0
        var title: String? = null
        var description: String? = null
        var price = 0
    }
}

fun View.addPlaceholder() {
    PlaceholderParameter.Builder()
        .setView(this)
        .setDrawable(
            BroccoliGradientDrawable(
                Color.parseColor("#DDDDDD"),
                Color.parseColor("#CCCCCC"),
                0f,
                1000,
                LinearInterpolator()
            )
        ).build()
}